// 云对象教程: https://uniapp.dcloud.net.cn/uniCloud/cloud-obj

const {
	rejects
} = require("assert")
const {
	resolve
} = require("dns")

// jsdoc语法提示教程：https://ask.dcloud.net.cn/docs/#//ask.dcloud.net.cn/article/129
module.exports = {
	_before: function() { // 通用预处理器

	},
	// 添加收藏
	add: async function(movie) {
		const db = uniCloud.database()
		let res = await db.collection("movieFav").add(movie)
		return {
			code: 1,
			msg: "成功收藏"
		}
	},
	// 判断是否已经收藏
	hasFav: async function(movie_id) {
		console.log("movie_id=" + movie_id);
		const db = uniCloud.database();
		let {
			total
		} = await db.collection("movieFav").where({
			movie_id: parseInt(movie_id)
		}).count()
		console.log(total);
		return {
			code: 1,
			count: total
		}
	},
	// 移除收藏
	removeFav: async function(movie_id) {
		const db = uniCloud.database()
		let {
			deleted
		} = await db.collection("movieFav").where({
			"movie_id": parseInt(movie_id)
		}).remove()
		return {
			code: 1,
			deleted
		}
	}
}